﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace ArkanoidX
{
    partial class Racket
    {
        public class FreeState : State
        {
            protected float _angle; // negative value for tilting to left, positive for right

            public FreeState(Texture2D texture, Vector2 size, Rectangle regionInSource, 
                Rectangle reachableArea)
                : base(texture, size, regionInSource, reachableArea)
            {
            }

            public FreeState(Texture2D texture, Vector2 size, Rectangle reachableArea)
                : base(texture, size, reachableArea)
            {
            }

            public FreeState(Texture2D texture, Rectangle reachableArea)
                : base(texture, reachableArea)
            {
            }

            public override void Draw(SpriteBatch sBatch, Vector2 position, Color color)
            {
                base.Draw(sBatch, position, color, _angle);
            }

            // Do update according to the state of the ship
            public override void Update(Racket racket, GameTime gameTime)
            {
                KeyboardState keyboard = Keyboard.GetState();

                _angle = 0.0f;

                if (keyboard.IsKeyDown(Keys.Left))
                {
                    racket._position.X -= 15;
                    _angle = 0.1f;
                }

                if (keyboard.IsKeyDown(Keys.Right))
                {
                    racket._position.X += 15;
                    _angle = -0.1f;
                }

                if (keyboard.IsKeyDown(Keys.Up))
                {
                    racket._position.Y -= 15;
                }

                if (keyboard.IsKeyDown(Keys.Down))
                {
                    racket._position.Y += 15;
                }

                // Keep the racket inside the screen
                if (racket._position.X < base._reachableArea.Left + base._halfSize.X)
                {
                    racket._position.X = base._reachableArea.Left + base._halfSize.X;
                }

                if (racket._position.X + base._halfSize.X > base._reachableArea.Right)
                {
                    racket._position.X = base._reachableArea.Right - base._halfSize.X;
                }

                if (racket._position.Y < base._reachableArea.Top + base._halfSize.Y)
                {
                    racket._position.Y = base._reachableArea.Top + base._halfSize.Y;
                }

                if (racket._position.Y > base._reachableArea.Bottom - base._halfSize.Y)
                {
                    racket._position.Y = base._reachableArea.Bottom - base._halfSize.Y;
                }

            }
        }
    }
}
